package com.pignest.hotel.ui.reception;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.service.RoomService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.L;

public class RemarkRoomWindow extends Window implements EventListener {
	private static final long serialVersionUID = 8535960359799405289L;
	
	private Label nameLabel;
	private Textbox remarkTextbox;
	private String roomId;
	private boolean success;
	
	public RemarkRoomWindow() {
		addEventListener(Events.ON_CREATE, this);
	}

	@Override
	public void onEvent(Event event) throws Exception {
		if (Events.ON_CREATE.equals(event.getName())) {
			nameLabel = (Label) getFellow("roomName");
			remarkTextbox = (Textbox) getFellow("remark");
			Room room = Services.getRoomService().getRoom(roomId);
			if (room != null) {
				nameLabel.setValue(room.getType().getName() + " " + room.getName());
				remarkTextbox.setValue(room.getRemark());
			}
		}
	}
	
	public void onSave() {
		RoomService service = Services.getRoomService();
		Room room = service.getRoom(roomId);
		if (room == null)
			return;
		String remark = remarkTextbox.getValue();
		if (!Utils.equals(remark, room.getRemark())) {
			room.setRemark(remark);
			service.update(room);
			L.update(room, "修改备注");
			success = true;
		}
		detach();
	}
	
	public boolean isSuccess() {
		return success;
	}
	
	public void setRoomId(String roomId) {
		this.roomId = roomId;
	}
}
